
from rest_framework.serializers import Serializer,ModelSerializer, ValidationError
from .models import Commodity


class CommoditySerializer(ModelSerializer):
    class Meta:
        model = Commodity
        fields = ['name', 'price']
    # 可以重写父类的方法

    # 在保存对象时调用
    def create(self, validated_data):
        # print('create student')
        return super(CommoditySerializer, self).create(validated_data)

    # 在更新对象时调用
    def update(self, instance, validated_data):
        # print('updating student')
        return super(CommoditySerializer, self).update(instance, validated_data)

    # def validate_price(self, value):
    #     print(f'Now validate price value: {value} type {type(value)}')
    #     if float(value) <= 0:
    #         raise ValidationError("price must be positive")
    #     return value

    # def validate_name(self, value):
    #     print('Now validate name value')
    #     # if float(value) <= 0:
    #     #     raise ValidationError("price must be positive")
    #     return value
